package com.example.demo.pojo.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.example.demo.pojo.User;
import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;

import java.util.Collection;
import java.util.List;

/**
 * 重写用户详情类,Security
 * @author 1
 */
//    isAccountNonExpired 用户账户未过期
//            isAccountNonLocked 账户未被锁定
//            isCredentialsNonExpired 凭证未过期
//            isEnabled 是否可用
public class UserSecurity implements UserDetails, Serializable {
    private static final long serialVersionUID = 1L;
    private User user;
    private List<String> permission;
    private List<String> role;
    @JsonIgnore
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority>  simpleGrantedAuthority;

    public List<String> getRole() {
        return role;
    }

    public UserSecurity(User user, List<String> permission, List<String> role, List<SimpleGrantedAuthority> simpleGrantedAuthority) {
        this.user = user;
        this.permission = permission;
        this.role = role;
        this.simpleGrantedAuthority = simpleGrantedAuthority;
    }

    public void setRole(List<String> role) {
        this.role = role;
    }

    public List<String> getPermission() {
        return permission;
    }

    public void setPermission(List<String> permission) {
        this.permission = permission;
    }

    public List<SimpleGrantedAuthority> getSimpleGrantedAuthority() {
        return simpleGrantedAuthority;
    }

    public void setSimpleGrantedAuthority(List<SimpleGrantedAuthority> simpleGrantedAuthority) {
        this.simpleGrantedAuthority = simpleGrantedAuthority;
    }

    public UserSecurity(User user, List<String> permission) {
        this.user = user;
        this.permission = permission;
    }

    public UserSecurity(User user) {
        this.user = user;
    }

    public UserSecurity() {
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return user.getPas();
    }

    @Override
    public String getUsername() {
        return user.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
